﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;

namespace AdventureWorksImplementation
{
    public class EntityService : IDisposable
    {

        private readonly AdventureWorksModelContainer _context = new AdventureWorksModelContainer();

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="AdventureWorksEntityType"></typeparam>
        /// <param name="productId"></param>
        /// <param name="workOrderIds"></param>
        /// <param name="locationId"></param>
        /// <param name="passThroughEntityName">See Comments above EntityModelNavigator.BuildShortestChain</param>
        /// <returns></returns>
        public IQueryable<AdventureWorksEntityType> All<AdventureWorksEntityType>( int productId, List<int> workOrderIds = null, int? locationId = null, string passThroughEntityName = "" )
            where AdventureWorksEntityType : class, new()
        {
            return new DynamicFilterWrapper( _context, productId, workOrderIds, locationId ).FilteredEntities<AdventureWorksEntityType>( passThroughEntityName );
        }

        public void Dispose()
        {
            Dispose( true );
            GC.SuppressFinalize( this );
        }

        protected virtual void Dispose( bool disposing )
        {
            if ( disposing )
            {
                if ( _context != null )
                {
                    _context.Dispose();
                }
            }
        }

        ~EntityService()
        {
            Dispose( false );
        }

    }
}
